﻿using System;
using System.Windows;
using Baosight.Platform.Core;
using Baosight.Platform.Core.UI.Controls;
using Baosight.ZNH.ZMCommon;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Baosight.ZNH.ZMCommon.Prompt;

namespace Baosight.ZNH.ZMCP.UI.PopWindows
{
    public partial class ProfessionalPlanDetailCM : EFViewCmes
    {
        public ProfessionalPlanDetailCM(string dialogType)
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
            Init(dialogType);
        }

        /// <summary>
        /// 初始化父类EFViewCmesProperty的值，每次点击“确定”、“返回”时执行此方法，从新赋值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.DataFields = this.spWindows.GetChildObjects<EFDataField>(null, true);
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 初始化页面数据
        /// </summary>
        /// <param name="type"></param>
        private void Init(string type)
        {
            type = type == null ? "insert" : type.ToLower();

            switch (type)
            {
                case "insert":
                    base.Mode = OperationMode.Insert;
                    break;
                case "update":
                    base.Mode = OperationMode.Update;
                    break;
                case "detail":
                    this.Commit.Visibility = Visibility.Collapsed;
                    BaseTools.EFDataFieldNotEnabled(this.spWindows.GetChildObjects<EFDataField>(null, true));
                    break;
                default:
                    break;
            }
        
        }
        /// <summary>
        /// 确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommitClick(object sender, RoutedEventArgs e)
        {
            //if (NotNullOrEmpty(this.fdBeginDate))
            //{
            //    string countBeginDay = this.fdCountBeginDate.Value.ToString();//生产统计月起始日

            //    int year = int.Parse(this.fdPlanYear.Value.ToString());//计划年份
            //    int month = int.Parse(this.fdPlanMonth.Value.ToString());//计划月份
            //    int day = int.Parse(countBeginDay);//生产统计月起始日

            //    if (month == 1)
            //    {
            //        month = 12;
            //        year = year - 1;
            //    }
            //    else
            //    {
            //        month = month - 1;
            //    }

            //    DateTime planStartDate = new DateTime(year, month, day);
            //    string planStartDateStr = planStartDate.ToString("yyyy-MM-dd");//计划开始日期

            //    DateTime planEndDate = planStartDate.AddMonths(1).AddDays(-1);
            //    string planEndDateStr = planEndDate.ToString("yyyy-MM-dd");//计划截止日期

            //    string beginDate = this.fdBeginDate.Value.ToString();//开始日期

            //    if (!(beginDate.CompareTo(planStartDateStr) >= 0 && beginDate.CompareTo(planEndDateStr) <= 0))
            //    {
            //        ValidateMessagePopup(this.fdBeginDate.Label + "不在计划日期范围内，请重新选择！");
            //        return;
            //    }
            //}
            if (NotNullOrEmpty(this.fdDesignSum) && NotNullOrEmpty(this.fdSurplusSum))
            {
                if (Convert.ToDouble(this.fdDesignSum.Value.ToString()) - Convert.ToDouble(this.fdSurplusSum.Value.ToString()) < 0)
                {
                    ValidateMessagePopup("剩余工作量不能大于设计工作量！");
                    return;
                }
            }

            base.OnCommitButtonClick(sender, e);
        }

        /// <summary>
        /// 判定EFDataField的值是否为空
        /// </summary>
        /// <param name="field"></param>
        /// <returns></returns>
        private bool NotNullOrEmpty(EFDataField field)
        {
            bool flag = false;

            if ((field.Value != null) && (!field.Value.ToString().Trim().Equals("")))
            {
                flag = true;
            }

            return flag;
        }

        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelClick(object sender, RoutedEventArgs e)
        {
            CmesWindows _window = this.Parent as CmesWindows;
            _window.DialogResult = false;
            _window.Close();
        }

        /// <summary>
        /// 工程值改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdProjectId_ValueChanged(object sender, Platform.Core.UI.Controls.ValueChangedEventArgs e)
        {
            EFComboBox select = (sender as EFDataField).Content as EFComboBox;

            this.fdDepartment.Value = select.SelectedItem.GetType().GetProperty("fdDepartment").GetValue(select.SelectedItem, null);
            this.fdDepartmentId.Value = select.SelectedItem.GetType().GetProperty("fdDepartmentId").GetValue(select.SelectedItem, null);
            this.fdCoalMethod.Value = select.SelectedItem.GetType().GetProperty("fdCoalTypeId").GetValue(select.SelectedItem, null);
            this.fdCoalType.Value = select.SelectedItem.GetType().GetProperty("fdRecoveryProcessId").GetValue(select.SelectedItem, null);
            this.fdFrameType.Value = select.SelectedItem.GetType().GetProperty("fdFrameType").GetValue(select.SelectedItem, null);
            this.fdRecoveryRate.Value = select.SelectedItem.GetType().GetProperty("fdChance").GetValue(select.SelectedItem, null);
            this.fdDesignSum.Value = select.SelectedItem.GetType().GetProperty("fdPlanAmount").GetValue(select.SelectedItem, null);
            this.fdThickness.Value = select.SelectedItem.GetType().GetProperty("fdThickness").GetValue(select.SelectedItem, null);
            this.fdBulkDensity.Value = select.SelectedItem.GetType().GetProperty("fdBulk").GetValue(select.SelectedItem, null);
            this.fdMineLength.Value = select.SelectedItem.GetType().GetProperty("fdLength").GetValue(select.SelectedItem, null);
            this.fdMineHight.Value = select.SelectedItem.GetType().GetProperty("fdHeight").GetValue(select.SelectedItem, null);
        }

        /// <summary>
        /// 施工部门
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdDepartmentId_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            EFComboBox box = (sender as EFDataField).Content as EFComboBox;
            if (box.SelectedItem != null)
            {
                this.fdDepartment.Value = box.SelectedItem.GetType().GetProperty("orgName").GetValue(box.SelectedItem, null);
            }

        }

        /// <summary>
        /// 计算日平均量
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdPlanSum_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            double planSum = 0.0;
            int planCrossDay = 0;
            if (this.fdPlanSum.Value != null)
            {
                Double.TryParse(this.fdPlanSum.Value.ToString(), out planSum);
            }
            if (this.fdPlanCrossDay != null)
            {
                Int32.TryParse(this.fdPlanCrossDay.Value.ToString(), out planCrossDay);
            }
            if (planCrossDay > 0)
            {
                this.fdDaySum.Value = Math.Round(planSum / planCrossDay, 0);//日平均量
            }
        }

        /// <summary>
        /// 验证失败，消息提示
        /// </summary>
        public void ValidateMessagePopup(string msg)
        {
            Prompts pop = Prompts.getInstance();
            pop.SetMsg(msg);
            pop.Show(6);

            return;
        }
    }
}
